#include "qoptionfiledialog.h"
#include <QtCore/QUrl>
#include <QtGui/QDesktopServices>
#include <QtGui/QGridLayout>
#include <QtGui/QLabel>


#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
extern bool qt_use_native_dialogs = false;
#endif


QOptionFileDialog::QOptionFileDialog(QWidget *parent, Qt::WindowFlags flags)
    : QFileDialog(parent, flags)
{
#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
    qt_use_native_dialogs = true;
#endif
    init();
}

QOptionFileDialog::QOptionFileDialog(QWidget *parent, const QString &caption, const QString &directory, const QString &filter)
    : QFileDialog(parent, caption, directory, filter)
{
#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
    qt_use_native_dialogs = true;
#endif
    init();
}

QOptionFileDialog::~QOptionFileDialog()
{
#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
    qt_use_native_dialogs = false;
#endif
}

void QOptionFileDialog::addOption(QLabel *label, QWidget *widget)
{
    QGridLayout* gridbox = qobject_cast<QGridLayout*>(layout());
    if (gridbox) {
        gridbox->addWidget(label);
        gridbox->addWidget(widget);
    }
}

void QOptionFileDialog::init()
{
    setViewMode(QFileDialog::List);

    QList<QUrl> urls;
    urls << QUrl::fromLocalFile("")
         << QUrl::fromLocalFile(QDir::currentPath())
         << QUrl::fromLocalFile(QDir::homePath());
#if (QT_VERSION >= QT_VERSION_CHECK(4, 4, 0))
    urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation))
         << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation))
         << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::PicturesLocation))
         << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::MusicLocation))
         << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::MoviesLocation));
#endif
    setSidebarUrls(urls);
}
